package com.dxq.test.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dxq.test.App;
import com.dxq.test.R;
import com.dxq.test.activity.GroupActivity;
import com.dxq.test.activity.GroupDetailActivity;
import com.dxq.test.activity.InventoryActivity;
import com.dxq.test.activity.LabelDetailActivity;
import com.dxq.test.activity.MainActivity;
import com.dxq.test.adapter.GroupAdapter;
import com.dxq.test.adapter.LabelAdapter;
import com.dxq.test.db.Event;
import com.dxq.test.db.Group;
import com.dxq.test.db.Label;
import com.dxq.test.db.Pasture;
import com.j256.ormlite.dao.ForeignCollection;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;


public class Fragment2_Inventory extends Fragment implements OnCheckedChangeListener, OnClickListener {
    final String TAG = "Fragment1";
    private View view;// this fragment UI

    private ImageView ivBack;
    private TextView tvTitle;
    private TextView tvSize;
    private RecyclerView recyclerView;
    private Button btnInventory, btnEvent;
    private GroupAdapter mAdapter;
    private List<Group> recordList = new ArrayList<>();

//    private Set<String> epcSet = null; //store different EPC
//    private List<EpcDataModel> listEpc = null;//EPC list
//    private Map<String, Integer> mapEpc = null; //store EPC position
//    private EPCadapter adapter;//epc list adapter

    //handler

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i("f1", "create view");
        view = inflater.inflate(R.layout.fragment_inventory, null);
        initView();
        initAdapter();
        return view/*super.onCreateView(inflater, container, savedInstanceState)*/;
    }


    private void initView() {
        ivBack = view.findViewById(R.id.ivBack);
        tvTitle = view.findViewById(R.id.tvTitle);
        ivBack.setVisibility(View.GONE);
        tvTitle.setText("分群计数");
        tvSize = view.findViewById(R.id.tvSize);
        tvSize.setText("0");
        recyclerView = view.findViewById(R.id.recyclerView);
    }

    private void initAdapter() {
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));//添加分割线
        mAdapter = new GroupAdapter(R.layout.item_group, recordList);//
        recyclerView.setAdapter(mAdapter);
        mAdapter.setEmptyView(R.layout.layout_empty);

        //item点击事件
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter,
                                    @NonNull @NotNull View view, int position) {
                //ToastUtils.showShort("item事件");
                //跳转页面
                Intent intent = new Intent(getActivity(), InventoryActivity.class);
                Group group = recordList.get(position);
                Bundle bundle = new Bundle();
                bundle.putSerializable("group", group);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        //item子view点击事件
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter,
                                         @NonNull @NotNull View view, int position) {
                //ToastUtils.showShort("child添加事件");

            }
        });
    }

    private void getData() {
        recordList.clear();
        Pasture pasture = ((MainActivity) getActivity()).tablesDao.queryPastureById(App.select_pasture_id);
        if (null != pasture) {
            ForeignCollection<Group> groups = pasture.getGroups();
            for (Group group : groups) {
                List<Label> labels = ((MainActivity) getActivity()).tablesDao.queryLabelForGroup(group);
                group.setLabelSize(labels.size());//群内牲畜数量
                recordList.add(group);
            }
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    private boolean f1hidden = false;
    private String TGA = "Fragment1";

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        f1hidden = hidden;
        Log.i("hidden", "hide" + hidden);
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {

        }

    }

    @Override
    public void onClick(View v) {

    }

    private void insertEvent(String time, String name) {

    }

    private void addDialog() {
        View view = getLayoutInflater().inflate(R.layout.add_event_dialog, null);
        AutoCompleteTextView eventTime = view.findViewById(R.id.time);
        AutoCompleteTextView eventName = view.findViewById(R.id.name);
        AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setTitle("添加事件")//设置对话框的标题
                .setView(view)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String time = eventTime.getText().toString();
                        String name = eventName.getText().toString();
                        if (time.equals("")) {
                            Toast.makeText(getActivity(), "填写事件名称", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (name.equals("")) {
                            Toast.makeText(getActivity(), "填写事件时间", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        insertEvent(time, name);
                    }
                }).create();
        dialog.show();
    }
}
